package wiz.client.storage;

import java.util.HashMap;
import java.util.Map;

import wiz.client.ClientFactory;
import wiz.client.WizGameStateStorage;
import wiz.client.model.WizGameModel;
import wiz.client.util.UUID;


public class PageSessionStateStorage implements WizGameStateStorage {

	Map<String, String> fStorage = new HashMap<String, String>();
	private ClientFactory fClientFactory;
	
	public PageSessionStateStorage(ClientFactory clientFactory) {
		fClientFactory = clientFactory;
	}

	@Override
	public String storeGameState(WizGameModel model) {
		String newID = UUID.uuid();
		String modelJSON = fClientFactory.getWizModelFactory().modelToJSON(model);
		fStorage.put(newID, modelJSON);
		return newID;
	}

	@Override
	public WizGameModel loadGameState(String stateToken) {
		String state = fStorage.get(stateToken);
		if(state == null){
			return null;
		} else {
			return fClientFactory.getWizModelFactory().jsonToModel(state);
		}
	}

}
